package com.android.job.logic;

import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;

import com.android.job.database.FormDBHelper;
import com.android.job.model.MyLocation;
import com.android.job.model.MyInfo;

public class MyData
{

	public FormDBHelper dbHelper;

	public static MyData instance;

	public MyData(Context context)
	{
		dbHelper = new FormDBHelper(context);
	}

	public void insertData(MyInfo request)
	{
		dbHelper.insertForm(request);
	}
	
	/**
	 * 插入签到位置
	 * @param loc
	 */
	public void insertLoc(MyLocation loc)
	{
		dbHelper.intertLocation(loc);
	}
	
	/**
	 * 获得所有签到位置
	 * @return
	 */
	public ArrayList<MyLocation> getLocations()
	{
		ArrayList<MyLocation> loc = dbHelper.getAllLocation();
		return loc;
	}
	

	public ArrayList<MyInfo> queryData(MyInfo request)
	{
		
		ArrayList<MyInfo> list = new ArrayList<MyInfo>();
		Cursor cursor = dbHelper.queryForm(request);
		try
		{
			if(null!=cursor)
			{
				if(cursor.moveToFirst())
				{
					do
					{
						MyInfo req = new MyInfo();
						req.id = cursor.getInt(cursor.getColumnIndex("id"));
						req.workId =cursor.getString(cursor.getColumnIndex("workId"));
						req.name = cursor.getString(cursor.getColumnIndex("name"));
						req.dept = cursor.getString(cursor.getColumnIndex("dept"));
						req.type = cursor.getString(cursor.getColumnIndex("type"));
						req.startTime = cursor.getString(cursor.getColumnIndex("begin"));
						req.endTime = cursor.getString(cursor.getColumnIndex("end"));
						req.days = cursor.getInt(cursor.getColumnIndex("days"));
						req.reason = cursor.getString(cursor.getColumnIndex("reason"));
						req.approve = cursor.getInt(cursor.getColumnIndex("approve"));
						list.add(req);
					}
					while (cursor.moveToNext());
				}
			}
		}
		catch (Exception e)
		{
		}finally
		{
			if(null!=cursor)
			{
			cursor.close();
			}
		}
	
		return list;
	}
	

	
	public int updateData(MyInfo request,int state)
	{
		int count = dbHelper.update(request,state);
		return count;
		
	}
	
	public ArrayList<MyInfo> queryData(int approve)
	{
		
		ArrayList<MyInfo> list = new ArrayList<MyInfo>();
		Cursor cursor = dbHelper.queryForm(approve);
		try
		{
			if(null!=cursor)
			{
				if(cursor.moveToFirst())
				{
					do
					{
						MyInfo req = new MyInfo();
						req.id = cursor.getInt(cursor.getColumnIndex("id"));
						req.workId =cursor.getString(cursor.getColumnIndex("workId"));
						req.name = cursor.getString(cursor.getColumnIndex("name"));
						req.dept = cursor.getString(cursor.getColumnIndex("dept"));
						req.type = cursor.getString(cursor.getColumnIndex("type"));
						req.startTime = cursor.getString(cursor.getColumnIndex("begin"));
						req.endTime = cursor.getString(cursor.getColumnIndex("end"));
						req.days = cursor.getInt(cursor.getColumnIndex("days"));
						req.reason = cursor.getString(cursor.getColumnIndex("reason"));
						req.approve = cursor.getInt(cursor.getColumnIndex("approve"));
						list.add(req);
					}
					while (cursor.moveToNext());
				}
			}
		}
		catch (Exception e)
		{
		}finally
		{
			if(null!=cursor)
			{
			cursor.close();
			}
		}
	
		return list;
	}
	

	public static MyData getInstance(Context context)
	{
		if (null == instance)
		{
			instance = new MyData(context);
		}
		return instance;
	}
}
